package leetcode.day_2020_11_16_11_22;

public class L_38 {
    public static void main(String[] args) {
        System.out.println(countAndSay(4));
    }

    public static String countAndSay(int n) {
        String str = "";
        StringBuilder num = new StringBuilder("1");
        for(int i=1; i<n; i++){
            num = solution(num+"");
        }
        return num+str;
    }

    private static StringBuilder solution(String num) {
        char[] strArr = num.toCharArray();
        char temp = strArr[0];
        int count = 0;
        StringBuilder str = new StringBuilder();
        for(int i=0; i<strArr.length; i++){
            if(strArr[i] != temp) {
                // 如果不同，说明遇到了新的数字
                str = str.append((count *10 + Integer.parseInt(String.valueOf(temp))));
                temp = strArr[i];
                count = 0;
            }
            count = count + 1;
            if(i == strArr.length-1)
                str =  str.append((count *10 + Integer.parseInt(String.valueOf(temp))));
        }
        return str;
    }
}
